/*
 * @Author: Y先森
 * @Date: 2023-05-04 11:54:39
 * @LastEditors: y && 250048395@qq.com
 * @LastEditTime: 2023-05-09 11:05:50
 * @FilePath: \y-website\src\store\modules\category.js
 */
import { getCategory , getCraftShareCategory , getCreationCategory} from '@/api'
import { ALL_CATEGORY_ITEM } from '@/constants'

export default {
  // 独立作用域
  namespaced: true,
  state: () => ({
    // 技术分享 数据源
    craftShareCategorys: '',
    // 灵感创作 数据源
    creationCategorys: {
        id:'0',
        name:'写作'
    },
    // 设计展示 数据源
    designCategorys: ''
  }),
  mutations: {
    /**
     * 分类赋值 - 【技术分享】
     */
    setCraftShareCategorys(state, craftCategorys) {
      state.craftShareCategorys = [ALL_CATEGORY_ITEM, ...craftCategorys]
    },
    /**
     * 分类赋值 - 【灵感创作】
     */
    setCreationCategorys(state, creationCategorys) {
      state.creationCategorys = creationCategorys
    },
    /**
     * 分类赋值 - 【设计展示】
     */
    setDesignCategorys(state, categorys) {
      state.designCategorys = [ALL_CATEGORY_ITEM, ...categorys]
    }
  },
  actions: {
    /**
     * 获取分类数据
     */
    async useCategoryData(context, parmas) {
      const keys = ['craftShare','creation','design']
      const result = keys.includes(parmas)
      if(result){       
        switch (parmas) {
          case 'craftShare':
            const { craftCategorys } = await getCraftShareCategory({type:parmas})
            context.commit('setCraftShareCategorys', craftCategorys)
            break
          case 'creation':
            const { creationCategorys } = await getCreationCategory({type:parmas})
            context.commit('setCreationCategorys', creationCategorys)
            break
          case 'design':
            const { categorys } = await getCategory({type:parmas})
            context.commit('setDesignCategorys', categorys)
            break
        }
      } else {
        throw new Error(`你分类类型必须是${keys.join('、')}中的一个`)
      }
    }
  }
}
